package com.xcity.record;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.xcity.merge.MergeUtil;

public class Employee extends BaseRecord {

	public static String SQL_PARA = "id, templateId, ownerId, state, exp, level, qualityLvl, star, energy, skills, attrs, equips, blessExp, blessLvl, createTime";

	public static String COUNT_SQL = "select count(id) from employee {0}";
	public static String SELECT_LIMIT = "select * from employee {2} limit {0},{1}";
	public static String CONDITION = "WHERE employee.ownerId not IN (SELECT player.id FROM player WHERE player.vipLvl = {0} AND player.`level` < {1} AND player.lastLoginTime < \"{2}\")";
	
	protected long id;
	protected int templateId;
	protected long ownerId;
	protected byte state;
	protected int exp;
	protected int level;
	protected int qualityLvl;
	protected int star;
	protected int energy;
	protected String skills;
	protected String attrs;
	protected String equips;
	protected String blessExp;
	protected String blessLvl;
	protected Date createTime;
	
	@Override
	public void load(ResultSet rs) throws SQLException {
		this.id = rs.getLong(1);
		this.templateId = rs.getInt(2);
		this.ownerId = rs.getLong(3);
		this.state = rs.getByte(4);
		this.exp = rs.getInt(5);
		this.level = rs.getInt(6);
		this.qualityLvl = rs.getInt(7);
		this.star = rs.getInt(8);
		this.energy = rs.getInt(9);
		this.skills = rs.getString(10);
		this.attrs = rs.getString(11);
		this.equips = rs.getString(12);
		this.blessExp = rs.getString(13);
		this.blessLvl = rs.getString(14);
		this.createTime = rs.getTimestamp(15);
	}

	@Override
	public String getColumnNames() {
		return SQL_PARA;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(MergeUtil.toSqlString(id));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(templateId));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(ownerId));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(state));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(exp));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(level));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(qualityLvl));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(star));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(energy));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(skills));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(attrs));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(equips));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(blessExp));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(blessLvl));
		sb.append(", ");
		sb.append(MergeUtil.toDateString(createTime));
		return sb.toString();
	}
	
	@Deprecated
	public static List<Employee> getEmployeeList(Connection conn, int start, int page) throws SQLException{
		List<Employee> ret = new ArrayList<Employee>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = MessageFormat.format(SELECT_LIMIT, start + "", page + "");
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Employee e = new Employee();
				e.load(rs);
				ret.add(e);
			}
		}finally{
			if(rs != null){
				rs.close();
			}
			if(ps != null){
				ps.close();
			}
		}
		return ret;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getTemplateId() {
		return templateId;
	}

	public void setTemplateId(int templateId) {
		this.templateId = templateId;
	}

	public long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(long ownerId) {
		this.ownerId = ownerId;
	}

	public byte getState() {
		return state;
	}

	public void setState(byte state) {
		this.state = state;
	}

	public int getExp() {
		return exp;
	}

	public void setExp(int exp) {
		this.exp = exp;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getQualityLvl() {
		return qualityLvl;
	}

	public void setQualityLvl(int qualityLvl) {
		this.qualityLvl = qualityLvl;
	}

	public int getStar() {
		return star;
	}

	public void setStar(int star) {
		this.star = star;
	}

	public int getEnergy() {
		return energy;
	}

	public void setEnergy(int energy) {
		this.energy = energy;
	}

	public String getSkills() {
		return skills;
	}

	public void setSkills(String skills) {
		this.skills = skills;
	}

	public String getAttrs() {
		return attrs;
	}

	public void setAttrs(String attrs) {
		this.attrs = attrs;
	}

	public String getEquips() {
		return equips;
	}

	public void setEquips(String equips) {
		this.equips = equips;
	}

	public String getBlessExp() {
		return blessExp;
	}

	public void setBlessExp(String blessExp) {
		this.blessExp = blessExp;
	}

	public String getBlessLvl() {
		return blessLvl;
	}

	public void setBlessLvl(String blessLvl) {
		this.blessLvl = blessLvl;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

}
